package com.icloud.admin.resource;


import com.github.pagehelper.PageInfo;
import com.icloud.admin.application.IdentityApplicationService;
import com.icloud.admin.application.command.AssignRoleToUserCommand;
import com.icloud.admin.application.command.ChangeUserPopsCommand;
import com.icloud.admin.application.command.CreateUserCommand;
import com.icloud.admin.query.IdentityQueryService;
import com.icloud.admin.query.dto.RoleDTO;
import com.icloud.admin.query.dto.UserDTO;
import com.icloud.admin.application.command.UndoAssignRoleToUserCommand;
import com.icloud.common.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private IdentityQueryService identityQueryService;

    @Autowired
    private IdentityApplicationService identityApplicationService;

    /**
     * 查询用户列表
     *
     * @param pageNum  页码
     * @param pageSize 页码记录数
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public InvokeResult pagingQueryUsers(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {

        return new InvokeResult(identityQueryService.findUsersPaging(pageNum, pageSize));
    }

    /**
     * 创建用户
     *
     * @param command 创建用户命令
     */
    @RequestMapping(method = RequestMethod.POST)
    public InvokeResult createUser(@Valid @RequestBody CreateUserCommand command) {
        identityApplicationService.createUser(command);
        return new InvokeResult();
    }

    /**
     * 查询用户详情
     *
     * @param userId 用户ID
     * @return 用户信息
     */
    @RequestMapping(value = "/{userId}",method = RequestMethod.GET)
    public InvokeResult findInfoOfUser(@PathVariable("userId") String userId) {
        return new InvokeResult(identityQueryService.findUserById(userId));
    }


    /**
     * 更新用户
     *
     * @param userId                用户id
     * @param changeUserPopsCommand 更新命令
     */
    @RequestMapping(value = "/{userId}" ,method = RequestMethod.PUT)
    public InvokeResult changeUserProps(@PathVariable("userId") String userId,@Valid @RequestBody ChangeUserPopsCommand changeUserPopsCommand) {
        changeUserPopsCommand.setUserId(userId);
        identityApplicationService.changeUserPops(changeUserPopsCommand);
        return new InvokeResult();
    }

    /**
     * 获取用户角色，标注分配和未分配
     *
     * @param userId 用户id
     * @return List<RoleDTO>
     */
    @RequestMapping(value = "/{userId}/roles", method = RequestMethod.GET)
    public InvokeResult queryUserRoles(@PathVariable("userId") String userId) {

        return new InvokeResult(identityQueryService.queryUserRoles(userId));
    }


    /**
     * 为用户授予角色
     *
     * @param userId                  用户id
     * @param assignRoleToUserCommand 分配角色命令
     */
    @RequestMapping(value = "/{userId}/roles", method = RequestMethod.POST)
    public InvokeResult assignRoleToUser(@PathVariable("userId") String userId,@Valid @RequestBody AssignRoleToUserCommand assignRoleToUserCommand) {
        assignRoleToUserCommand.setUserId(userId);
        identityApplicationService.assignRoleToUser(assignRoleToUserCommand);
        return new InvokeResult();
    }


    /**
     * 撤销拥有的角色
     *
     * @param userId                      用户id
     * @param undoAssignRoleToUserCommand 撤销分配角色命令
     */
    @RequestMapping(value = "/{userId}/roles", method = RequestMethod.DELETE)
    public InvokeResult undoAssignRoleToUser(@PathVariable("userId") String userId, @Valid @RequestBody UndoAssignRoleToUserCommand undoAssignRoleToUserCommand) {
        undoAssignRoleToUserCommand.setUserId(userId);
        identityApplicationService.undoAssignedRoleToUser(undoAssignRoleToUserCommand);
        return new InvokeResult();
    }

    @RequestMapping(value = "/{userId}/disable", method = RequestMethod.PUT)
    public InvokeResult undoAssignRoleToUser(@PathVariable("userId") String userId) {
        identityApplicationService.disableUser(userId);
        return new InvokeResult();
    }

    @RequestMapping(value = "/{userId}/activate", method = RequestMethod.PUT)
    public InvokeResult activateUser(@PathVariable("userId") String userId) {
        identityApplicationService.activateUser(userId);
        return new InvokeResult();
    }
}
